package com.infore.qualityControl.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.infore.qualityControl.bo.QualityParamSelectBo;
import com.infore.qualityControl.constant.Constants;
import com.infore.qualityControl.enums.QualityProjectEnum;
import com.infore.qualityControl.enums.ResultStatus;
import com.infore.qualityControl.enums.TechnicalCequirementEnum;
import com.infore.qualityControl.po.AutomaticAndManualComparison;
import com.infore.qualityControl.po.FiveParameter;
import com.infore.qualityControl.po.IntegratedIntervention;
import com.infore.qualityControl.po.MultipointLinearity;
import com.infore.qualityControl.po.RealWaterSampleComparison;
import com.infore.qualityControl.po.Recovery;
import com.infore.qualityControl.po.SpanDrift;
import com.infore.qualityControl.po.VerificationOfStandardSample;
import com.infore.qualityControl.po.ZeroDrift;
import com.infore.qualityControl.repository.IAutomaticAndManualComparisonRepository;
import com.infore.qualityControl.repository.IFiveParameterRepository;
import com.infore.qualityControl.repository.IIntegratedInterventionRepository;
import com.infore.qualityControl.repository.IMultipointLinearityRepository;
import com.infore.qualityControl.repository.IRealWaterSampleComparisonRepository;
import com.infore.qualityControl.repository.IRecoveryRepository;
import com.infore.qualityControl.repository.ISpanDriftRepository;
import com.infore.qualityControl.repository.IVerificationOfStandardSampleRepository;
import com.infore.qualityControl.repository.IZeroDriftRepository;
import com.infore.qualityControl.select.QueryDispatcher;
import com.infore.qualityControl.select.util.PageObject;
import com.infore.qualityControl.service.ITestDataService;
import com.infore.qualityControl.service.IVerificationService;
import com.infore.qualityControl.util.ArrayUtils;
import com.infore.qualityControl.util.DateUtil;
import com.infore.qualityControl.util.Page;
import com.infore.qualityControl.util.PageUtil;
import com.infore.qualityControl.util.ResultEntity;
import com.infore.qualityControl.util.ScaleUtil;

@Service
public class TestDataServiceImpl implements ITestDataService {

	@Autowired
	QueryDispatcher QueryDispatcher;

	@Autowired
	IZeroDriftRepository IZeroDriftRepository;

	@Autowired
	ISpanDriftRepository ISpanDriftRepository;

	@Autowired
	IFiveParameterRepository IFiveParameterRepository;

	@Autowired
	IAutomaticAndManualComparisonRepository IAutomaticAndManualComparisonRepository;

	@Autowired
	IMultipointLinearityRepository IMultipointLinearityRepository;

	@Autowired
	IRealWaterSampleComparisonRepository IRealWaterSampleComparisonRepository;

	@Autowired
	IIntegratedInterventionRepository IIntegratedInterventionRepository;

	@Autowired
	IRecoveryRepository IRecoveryRepository;

	@Autowired
	IVerificationOfStandardSampleRepository IVerificationOfStandardSampleRepository;

	@Autowired
	IVerificationService IVerificationService;
	
	public static final String QUALIFIED = "合格";
	public static final String NO_QUALIFIED = "不合格";

	@SuppressWarnings("unchecked")
	@Override
	public ResultEntity zeroDriftData(QualityParamSelectBo qualityParamSelectBo, PageObject pageObject) {
		boolean pageFlag = pageObject != null && pageObject.getPageNumber() != null && pageObject.getPageSize() != null;
		// 验证参数
		ResultEntity resultEntity = IVerificationService.verification(qualityParamSelectBo);
		if (resultEntity.getCode() == 1) {
			return resultEntity;
		}
		Date startDate = DateUtil.stringToDate(qualityParamSelectBo.getStartTime(), DateUtil.DATE_SMALL_YMDHMS);
		Date endDate = DateUtil.stringToDate(qualityParamSelectBo.getEndTime(), DateUtil.DATE_SMALL_YMDHMS);
		// 字符串转集合
		List<String> indexList = ArrayUtils.stringToList(qualityParamSelectBo.getIndexCodes());
		List<String> stationCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
		// 获取数据集
		List<ZeroDrift> list = IZeroDriftRepository.findByTimeBetweenAndIndexCodeInAndStationCodeInOrderByTimeDesc(
				startDate, endDate, indexList, stationCodeList);
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		Map<String, Object> valueMap = new HashMap<String, Object>();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 数据封装
		for (ZeroDrift zeroDrift : list) {
			String stationCode = zeroDrift.getStationCode();
			String time = DateUtil.dateToString(zeroDrift.getTime(), DateUtil.DATE_SMALL_YMDHMS);
			String indexCode = zeroDrift.getIndexCode();
			if (!valueMap.containsKey(stationCode + time)) {
				resultMap = new HashMap<String, Object>();
				resultMap.put("time", time);
				result.add(resultMap);
				valueMap.put(stationCode + time, resultMap);
			} else {
				resultMap = (Map<String, Object>) valueMap.get(stationCode + time);
			}
			resultMap.put(indexCode+"value", zeroDrift.getValue());
			resultMap.put(indexCode+"standardSamplesValue", zeroDrift.getStandardSamplesValue());
			resultMap.put(indexCode+"absoluteError", zeroDrift.getAbsoluteError());
			resultMap.put(indexCode+"qualification", zeroDrift.getQualification());
			resultMap.put(indexCode+"lastValue", zeroDrift.getLastValue());
			resultMap.put(indexCode+"spanValue", zeroDrift.getSpanValue());
			resultMap.put(indexCode+"relativeError", zeroDrift.getRelativeError());
			resultMap.put(indexCode+"lastQualification", zeroDrift.getLastQualification());
		}
		// 对数据进行简要结论
		int qualifiedCount = 0, noQualifiedCount = 0;
		for (Map<String, Object> map : result) {
			int codeCount = 0, noCodeCount = 0; // 记录每一行是否合格
			for (String code : indexList) {
				String qualification = map.get(code+"qualification") == null ? "" : map.get(code+"qualification").toString();
				String lastQualification = map.get(code+"lastQualification") == null ? "" : map.get(code+"lastQualification").toString();
				if (NO_QUALIFIED.equals(qualification) || NO_QUALIFIED.equals(lastQualification) || 
						"".equals(qualification) || "".equals(lastQualification)) { // 只要有一个因子的漂移或核查不合格，则都不合格
					noCodeCount = 1;
					break;
				} else {
					codeCount = 1;
				}
			}
			noQualifiedCount += noCodeCount;
			qualifiedCount += codeCount;
		}
		if (pageFlag) {
			Page<Map<String, Object>> page = PageUtil.inventedPage(result, pageObject);
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setPage(page)
					.setMapValue("count", result.size()).setMapValue("qualifiedCount", qualifiedCount)
					.setMapValue("noQualifiedCount", noQualifiedCount);
		}
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(result)
				.setMapValue("count", result.size()).setMapValue("qualifiedCount", qualifiedCount)
				.setMapValue("noQualifiedCount", noQualifiedCount);
	}

	@Override
	public ResultEntity zeroDriftChartData(QualityParamSelectBo qualityParamSelectBo) {
		// 验证参数
		ResultEntity resultEntity = IVerificationService.verification(qualityParamSelectBo);
		if (resultEntity.getCode() == 1) {
			return resultEntity;
		}
		// 对时间进行小时加减并放入集合中（作为图表x轴的数据）
		// List<String> timeList =
		// DateUtil.getTimeList(qualityParamSelectBo.getStartTime(),
		// qualityParamSelectBo.getEndTime(),
		// DateUtil.DATE_SMALL_YMDHMS, "day");
		Date startDate = DateUtil.stringToDate(qualityParamSelectBo.getStartTime(), DateUtil.DATE_SMALL_YMDHMS);
		Date endDate = DateUtil.stringToDate(qualityParamSelectBo.getEndTime(), DateUtil.DATE_SMALL_YMDHMS);
		// 字符串转集合
		List<String> indexList = ArrayUtils.stringToList(qualityParamSelectBo.getIndexCodes());
		List<String> stationCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
		// 获取数据集
		List<ZeroDrift> list = IZeroDriftRepository.findByTimeBetweenAndIndexCodeInAndStationCodeInOrderByTimeAsc(
				startDate, endDate, indexList, stationCodeList);
		// 数据封装
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(); // 返回的结果集
		for (String string : indexList) { // 前端传入的监测项目遍历
			Map<String, Object> map = new HashMap<String, Object>();
			// X轴数据
			List<String> xAxisList = list.stream().filter(z -> string.equals(z.getIndexCode()))
					.map(z -> DateUtil.dateToString(z.getTime(), DateUtil.DATE_SMALL_YMDHMS))
					.collect(Collectors.toList());
			// Y轴数据
			List<Double> yAxisList = list.stream().filter(z -> string.equals(z.getIndexCode()))
					.map(z -> "-".equals(z.getRelativeError()) ? null : Double.valueOf(z.getRelativeError()))
					.collect(Collectors.toList());
			// 对数据进行null补值(某个时间点没有值)
			// for (int i = 0; i < timeList.size(); i++) {
			// if (!xAxisList.contains(timeList.get(i))) {
			// yAxisList.add(i, null);
			// }
			// }
			map.put("name", QualityProjectEnum.getName(string));
			map.put("xAxis", xAxisList);
			map.put("yAxis", yAxisList);
			map.put("upper", TechnicalCequirementEnum.ZERO_DRIFT_UPPER.getCode());
			map.put("lower", TechnicalCequirementEnum.ZERO_DRIFT_LOWER.getCode());
			result.add(map);
		}
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(result);
	}

	@SuppressWarnings("unchecked")
	@Override
	public ResultEntity spanDriftData(QualityParamSelectBo qualityParamSelectBo, PageObject pageObject) {
		boolean pageFlag = pageObject != null && pageObject.getPageNumber() != null && pageObject.getPageSize() != null;
		// 验证参数
		ResultEntity resultEntity = IVerificationService.verification(qualityParamSelectBo);
		if (resultEntity.getCode() == 1) {
			return resultEntity;
		}
		Date startDate = DateUtil.stringToDate(qualityParamSelectBo.getStartTime(), DateUtil.DATE_SMALL_YMDHMS);
		Date endDate = DateUtil.stringToDate(qualityParamSelectBo.getEndTime(), DateUtil.DATE_SMALL_YMDHMS);
		List<String> indexList = ArrayUtils.stringToList(qualityParamSelectBo.getIndexCodes());
		List<String> stationCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
		// 查询数据
		List<SpanDrift> list = ISpanDriftRepository.findByTimeBetweenAndIndexCodeInAndStationCodeInOrderByTimeDesc(
				startDate, endDate, indexList, stationCodeList);
		// 封装数据
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		Map<String, Object> valueMap = new HashMap<String, Object>();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		for (SpanDrift spanDrift : list) {
			String stationCode = spanDrift.getStationCode();
			String time = DateUtil.dateToString(spanDrift.getTime(), DateUtil.DATE_SMALL_YMDHMS);
			String indexCode = spanDrift.getIndexCode();
			if (!valueMap.containsKey(stationCode + time)) {
				resultMap = new HashMap<String, Object>();
				resultMap.put("time", time);
				result.add(resultMap);
				valueMap.put(stationCode + time, resultMap);
			} else {
				resultMap = (Map<String, Object>) valueMap.get(stationCode + time);
			}
			resultMap.put(indexCode+"value", spanDrift.getValue());
			resultMap.put(indexCode+"standardSamplesValue", spanDrift.getStandardSamplesValue());
			resultMap.put(indexCode+"checkRelativeError", spanDrift.getCheckRelativeError());
			resultMap.put(indexCode+"qualification", spanDrift.getQualification());
			resultMap.put(indexCode+"lastValue", spanDrift.getLastValue());
			resultMap.put(indexCode+"spanValue", spanDrift.getSpanValue());
			resultMap.put(indexCode+"driftRelativeError", spanDrift.getDriftRelativeError());
			resultMap.put(indexCode+"lastQualification", spanDrift.getLastQualification());
		}
		// 对数据进行简要结论
		int qualifiedCount = 0, noQualifiedCount = 0;
		for (Map<String, Object> map : result) {
			int codeCount = 0, noCodeCount = 0; // 记录每一行是否合格
			for (String code : indexList) {
				String qualification = map.get(code+"qualification") == null ? "" : map.get(code+"qualification").toString();
				String lastQualification = map.get(code+"lastQualification") == null ? "" : map.get(code+"lastQualification").toString();
				if (NO_QUALIFIED.equals(qualification) || NO_QUALIFIED.equals(lastQualification) || 
						"".equals(qualification) || "".equals(lastQualification)) { // 只要有一个因子的漂移或核查不合格，则都不合格
					noCodeCount = 1;
					break;
				} else {
					codeCount = 1;
				}
			}
			noQualifiedCount += noCodeCount;
			qualifiedCount += codeCount;
		}
		if (pageFlag) {
			Page<Map<String, Object>> page = PageUtil.inventedPage(result, pageObject);
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setPage(page)
					.setMapValue("count", result.size()).setMapValue("qualifiedCount", qualifiedCount)
					.setMapValue("noQualifiedCount", noQualifiedCount);
		}
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(result)
				.setMapValue("count", result.size()).setMapValue("qualifiedCount", qualifiedCount)
				.setMapValue("noQualifiedCount", noQualifiedCount);
	}

	@Override
	public ResultEntity spanDriftChartData(QualityParamSelectBo qualityParamSelectBo) {
		// 验证参数
		ResultEntity resultEntity = IVerificationService.verification(qualityParamSelectBo);
		if (resultEntity.getCode() == 1) {
			return resultEntity;
		}
		// 对时间进行小时加减并放入集合中（作为图表x轴的数据）
		// List<String> timeList =
		// DateUtil.getTimeList(qualityParamSelectBo.getStartTime(),
		// qualityParamSelectBo.getEndTime(),
		// DateUtil.DATE_SMALL_YMDHMS, "day");
		Date startDate = DateUtil.stringToDate(qualityParamSelectBo.getStartTime(), DateUtil.DATE_SMALL_YMDHMS);
		Date endDate = DateUtil.stringToDate(qualityParamSelectBo.getEndTime(), DateUtil.DATE_SMALL_YMDHMS);
		List<String> indexList = ArrayUtils.stringToList(qualityParamSelectBo.getIndexCodes());
		List<String> stationList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
		// 查询数据
		List<SpanDrift> list = ISpanDriftRepository.findByTimeBetweenAndIndexCodeInAndStationCodeInOrderByTimeAsc(
				startDate, endDate, indexList, stationList);
		// 数据封装
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(); // 返回的结果集
		for (String string : indexList) { // 前端传入的监测项目遍历
			Map<String, Object> map = new HashMap<String, Object>();
			// X轴数据
			List<String> xAxisList = list.stream().filter(z -> string.equals(z.getIndexCode()))
					.map(z -> DateUtil.dateToString(z.getTime(), DateUtil.DATE_SMALL_YMDHMS))
					.collect(Collectors.toList());
			// Y轴数据
			List<Double> yAxisList = list.stream().filter(z -> string.equals(z.getIndexCode()))
					.map(z -> "-".equals(z.getDriftRelativeError()) ? null : Double.valueOf(z.getDriftRelativeError()))
					.collect(Collectors.toList());
			// 对数据进行null补值(某个时间点没有值)(时间不是规律性的,无法比较)
			// for (int i = 0; i < timeList.size(); i++) {
			// if (!xAxisList.contains(timeList.get(i))) {
			// yAxisList.add(i, null);
			// }
			// }
			map.put("name", QualityProjectEnum.getName(string));
			map.put("xAxis", xAxisList);
			map.put("yAxis", yAxisList);
			map.put("upper", TechnicalCequirementEnum.SPAN_DRIFT_UPPER.getCode());
			map.put("lower", TechnicalCequirementEnum.SPAN_DRIFT_LOWER.getCode());
			result.add(map);
		}
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(result);
	}

	@SuppressWarnings("unchecked")
	@Override
	public ResultEntity fiveParameterData(QualityParamSelectBo qualityParamSelectBo, PageObject pageObject) {
		boolean pageFlag = pageObject != null && pageObject.getPageNumber() != null && pageObject.getPageSize() != null;
		// 验证参数
		ResultEntity resultEntity = IVerificationService.verification(qualityParamSelectBo);
		if (resultEntity.getCode() == 1) {
			return resultEntity;
		}
		Date startDate = DateUtil.stringToDate(qualityParamSelectBo.getStartTime(), DateUtil.DATE_SMALL_STR);
		Date endDate = DateUtil.stringToDate(qualityParamSelectBo.getEndTime(), DateUtil.DATE_SMALL_STR);
		List<String> indexList = ArrayUtils.stringToList(qualityParamSelectBo.getIndexCodes());
		List<String> stationCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
		List<FiveParameter> list = null;
		Page<FiveParameter> page = null;
		// 当前断面
		if (qualityParamSelectBo.getIfCurrentSection()) {
			if (pageFlag) {
				// 此处不直接返回,是因为要处理数据,并且要查询出所有的数据,故不用else结尾
				page = PageUtil.pageConvert(
						IFiveParameterRepository.findByTimeBetweenAndIndexCodeInAndStationCodeInOrderByTimeDesc(
								startDate, endDate, indexList, stationCodeList, pageObject.getPageRequest()));
			}
			list = IFiveParameterRepository.findByTimeBetweenAndIndexCodeInAndStationCodeInOrderByTimeDesc(startDate,
					endDate, indexList, stationCodeList);
		} else {
			// 前端传入区域编码以站点编码参数名传递即可
			if (pageFlag) {
				page = PageUtil.pageConvert(
						IFiveParameterRepository.findByTimeBetweenAndIndexCodeInAndAreaCodeInOrderByTimeDesc(startDate,
								endDate, indexList, stationCodeList, pageObject.getPageRequest()));
			}
			list = IFiveParameterRepository.findByTimeBetweenAndIndexCodeInAndAreaCodeInOrderByTimeDesc(startDate,
					endDate, indexList, stationCodeList);
		}
		// 对数据进行简要结论
		Map<String, Object> valueMap = new HashMap<String, Object>();
		List<Object> verdictList = new ArrayList<Object>(); // 收集每个站点每个时间的各因子合格情况
		for (FiveParameter fiveParameter : list) {
			String stationCode = fiveParameter.getStationCode();
			Date date = fiveParameter.getTime();
			String qualification = fiveParameter.getQualification();
			if (!valueMap.containsKey(stationCode + date)) {
				List<String> qualifiedList = new ArrayList<String>();
				valueMap.put(stationCode + date, qualifiedList);
				qualifiedList.add(qualification);
				verdictList.add(qualifiedList);
			} else {
				List<String> qualifiedList = (List<String>) valueMap.get(stationCode + date);
				qualifiedList.add(qualification);
			}
		}
		int qualifiedCount = 0, noQualifiedCount = 0;
		for (Object object : verdictList) {
			List<String> qualifiedList = (List<String>) object;
			if (qualifiedList.contains(NO_QUALIFIED)) { // 只要一个站点的一个时间点内有一个因子不合格则代表不合格
				noQualifiedCount++;
			} else {
				qualifiedCount++;
			}
		}
		if (pageFlag) {
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setPage(page)
					.setMapValue("count", verdictList.size()).setMapValue("qualifiedCount", qualifiedCount)
					.setMapValue("noQualifiedCount", noQualifiedCount);
		}
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list)
				.setMapValue("count", verdictList.size()).setMapValue("qualifiedCount", qualifiedCount)
				.setMapValue("noQualifiedCount", noQualifiedCount);
	}

	@Override
	public ResultEntity fiveParameterChartData(QualityParamSelectBo qualityParamSelectBo) {
		// 验证参数
		ResultEntity resultEntity = IVerificationService.verification(qualityParamSelectBo);
		if (resultEntity.getCode() == 1) {
			return resultEntity;
		}
		Date startDate = DateUtil.stringToDate(qualityParamSelectBo.getStartTime(), DateUtil.DATE_SMALL_STR);
		Date endDate = DateUtil.stringToDate(qualityParamSelectBo.getEndTime(), DateUtil.DATE_SMALL_STR);
		List<String> indexList = ArrayUtils.stringToList(qualityParamSelectBo.getIndexCodes());
		List<String> stationCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
		List<FiveParameter> list = IFiveParameterRepository
				.findByTimeBetweenAndIndexCodeInAndStationCodeInOrderByTimeAsc(startDate, endDate, indexList,
						stationCodeList);
		// 数据封装
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(); // 返回的结果集
		for (String string : indexList) { // 前端传入的监测项目遍历
			Map<String, Object> map = new HashMap<String, Object>();
			// X轴数据
			List<String> xAxisList = list.stream().filter(z -> string.equals(z.getIndexCode()))
					.map(z -> DateUtil.dateToString(z.getTime(), DateUtil.DATE_FULL_STR)).collect(Collectors.toList());
			// Y轴数据
			List<Double> yAxisList = list.stream().filter(z -> string.equals(z.getIndexCode()))
					.map(z -> Double.valueOf(z.getError())).collect(Collectors.toList());
			map.put("name", QualityProjectEnum.getName(string));
			map.put("xAxis", xAxisList);
			map.put("yAxis", yAxisList);
			map.put("upper", TechnicalCequirementEnum.SPAN_DRIFT_UPPER.getCode());
			map.put("lower", TechnicalCequirementEnum.SPAN_DRIFT_LOWER.getCode());
			result.add(map);
		}
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(result);
	}

	@SuppressWarnings("unchecked")
	@Override
	public ResultEntity automaticAndManualComparisonData(QualityParamSelectBo qualityParamSelectBo,
			PageObject pageObject) {
		boolean pageFlag = pageObject != null && pageObject.getPageNumber() != null && pageObject.getPageSize() != null;
		// 验证参数
		ResultEntity resultEntity = IVerificationService.verification(qualityParamSelectBo);
		if (resultEntity.getCode() == 1) {
			return resultEntity;
		}
		Date startDate = DateUtil.stringToDate(qualityParamSelectBo.getStartTime(), DateUtil.DATE_SMALL_STR);
		Date endDate = DateUtil.stringToDate(qualityParamSelectBo.getEndTime(), DateUtil.DATE_SMALL_STR);
		List<String> indexList = ArrayUtils.stringToList(qualityParamSelectBo.getIndexCodes());
		List<String> stationCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
		// 查询数据
		List<AutomaticAndManualComparison> list = IAutomaticAndManualComparisonRepository
				.findByTimeBetweenAndIndexCodeInAndStationCodeInOrderByTimeDesc(startDate, endDate, indexList,
						stationCodeList);
		// 封装数据
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		Map<String, Object> valueMap = new HashMap<String, Object>();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		for (AutomaticAndManualComparison automaticAndManualComparison : list) {
			String time = DateUtil.dateToString(automaticAndManualComparison.getTime(), DateUtil.DATE_FULL_STR);
			String indexCode = automaticAndManualComparison.getIndexCode();
			String stationCode = automaticAndManualComparison.getStationCode();
			resultMap = new HashMap<String, Object>();
			if (!valueMap.containsKey(stationCode + time)) {
				resultMap.put(indexCode, automaticAndManualComparison);
				result.add(resultMap);
				valueMap.put(stationCode + time, resultMap);
			} else {
				resultMap = (Map<String, Object>) valueMap.get(stationCode + time);
				resultMap.put(indexCode, automaticAndManualComparison);
			}
		}
		if (pageFlag) {
			// 获取分页结果
			Page<Map<String, Object>> page = PageUtil.inventedPage(result, pageObject);
			// 获取分页后的数据集
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setPage(page);
		}
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(result);
	}

	@Override
	public ResultEntity multipointlinearityData(QualityParamSelectBo qualityParamSelectBo, PageObject pageObject) {
		boolean pageFlag = pageObject.getPageNumber() != null && pageObject.getPageSize() != null;
		// 验证参数
		ResultEntity resultEntity = IVerificationService.verification(qualityParamSelectBo);
		if (resultEntity.getCode() == 1) {
			return resultEntity;
		}
		// 只针对年月日处理
		Date startDate = DateUtil.dangyueDiyitianDay(
				DateUtil.stringToDate(qualityParamSelectBo.getStartTime(), DateUtil.DATE_SMALL_STR),
				DateUtil.DATE_FULL_STR);
		Date endDate = DateUtil.getLastDayDayM(
				DateUtil.stringToDate(qualityParamSelectBo.getEndTime(), DateUtil.DATE_SMALL_STR),
				DateUtil.DATE_FULL_STR);
		List<String> indexList = ArrayUtils.stringToList(qualityParamSelectBo.getIndexCodes());
		List<String> stationCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
		List<MultipointLinearity> list = null;
		// 当前断面
		if (qualityParamSelectBo.getIfCurrentSection()) {
			if (pageFlag) {
				Page<MultipointLinearity> page = PageUtil.pageConvert(IMultipointLinearityRepository
						.findByTimeBetweenAndIndexCodeInAndStationCodeInOrderByIndexCodeDesc(startDate, endDate,
								indexList, stationCodeList, pageObject.getPageRequest()));
				return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setPage(page);
			}
			list = IMultipointLinearityRepository.findByTimeBetweenAndIndexCodeInAndStationCodeInOrderByIndexCodeDesc(
					startDate, endDate, indexList, stationCodeList);
		} else {
			if (pageFlag) {
				Page<MultipointLinearity> page = PageUtil.pageConvert(
						IMultipointLinearityRepository.findByTimeBetweenAndIndexCodeInAndAreaCodeInOrderByIndexCodeDesc(
								startDate, endDate, indexList, stationCodeList, pageObject.getPageRequest()));
				return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setPage(page);
			}
			list = IMultipointLinearityRepository.findByTimeBetweenAndIndexCodeInAndAreaCodeInOrderByIndexCodeDesc(
					startDate, endDate, indexList, stationCodeList);
		}
		// 需要计算相关系数
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
	}

	@SuppressWarnings("unchecked")
	@Override
	public ResultEntity realWaterSampleComparisonData(QualityParamSelectBo qualityParamSelectBo,
			PageObject pageObject) {
		boolean pageFlag = pageObject != null && pageObject.getPageNumber() != null && pageObject.getPageSize() != null;
		// 验证参数
		ResultEntity resultEntity = IVerificationService.verification(qualityParamSelectBo);
		if (resultEntity.getCode() == 1) {
			return resultEntity;
		}
		Date startDate = DateUtil.dangyueDiyitianMonth(
				DateUtil.stringToDate(qualityParamSelectBo.getStartTime(), DateUtil.DATE_SMALL_YM),
				DateUtil.DATE_FULL_STR);
		Date endDate = DateUtil.getLastDayMonthM(
				DateUtil.stringToDate(qualityParamSelectBo.getEndTime(), DateUtil.DATE_SMALL_YM),
				DateUtil.DATE_FULL_STR);
		List<String> indexList = ArrayUtils.stringToList(qualityParamSelectBo.getIndexCodes());
		List<String> stationCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
		// 获取查询数据
		List<RealWaterSampleComparison> list = IRealWaterSampleComparisonRepository
				.findByTimeBetweenAndIndexCodeInAndStationCodeInOrderByTimeDesc(startDate, endDate, indexList,
						stationCodeList);
		// 所有断面
		if (!qualityParamSelectBo.getIfCurrentSection()) {
			list = IRealWaterSampleComparisonRepository.findByTimeBetweenAndIndexCodeInAndAreaCodeInOrderByTimeDesc(
					startDate, endDate, indexList, stationCodeList);
			Map<String, Object> valueMap = new HashMap<String, Object>();
			Map<String, Object> resultMap = new HashMap<String, Object>();
			List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
			for (RealWaterSampleComparison realWaterSampleComparison : list) {
				String areaName = realWaterSampleComparison.getAreaName();
				String basinName = realWaterSampleComparison.getBasinName();
				String stationName = realWaterSampleComparison.getStationName();
				String stationCode = realWaterSampleComparison.getStationCode();
				String indexCode = realWaterSampleComparison.getIndexCode();
				String time = DateUtil.dateToString(realWaterSampleComparison.getTime(), DateUtil.DATE_FULL_STR);
				String value = realWaterSampleComparison.getAutomaticValue(); // 仪器测试值
				if (!valueMap.containsKey(stationCode + time)) {
					resultMap = new HashMap<String, Object>();
					resultMap.put("areaName", areaName);
					resultMap.put("basinName", basinName);
					resultMap.put("stationName", stationName);
					resultMap.put("time", time);
					resultMap.put(indexCode, value);
					result.add(resultMap);
					valueMap.put(stationCode + time, resultMap);
				} else {
					resultMap = (Map<String, Object>) valueMap.get(stationCode + time);
					resultMap.put(indexCode, value);
				}
			}
			if (pageFlag) {
				Page<Map<String, Object>> page = PageUtil.inventedPage(result, pageObject);
				return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setPage(page);
			}
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(result);
		}
		// 当前断面封装数据
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		Map<String, Object> valueMap = new HashMap<String, Object>();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		for (RealWaterSampleComparison realWaterSampleComparison : list) {
			String stationCode = realWaterSampleComparison.getStationCode();
			String samplingTime = DateUtil.dateToString(realWaterSampleComparison.getSamplingTime(),
					DateUtil.DATE_SMALL_YMDHH);
			if (!valueMap.containsKey(stationCode + samplingTime)) {
				List<RealWaterSampleComparison> realList = new ArrayList<RealWaterSampleComparison>();
				realList.add(realWaterSampleComparison);
				resultMap = new HashMap<String, Object>();
				resultMap.put("time", samplingTime);
				resultMap.put("list", realList);
				result.add(resultMap);
				valueMap.put(stationCode + samplingTime, resultMap);
			} else {
				resultMap = (Map<String, Object>) valueMap.get(stationCode + samplingTime);
				List<RealWaterSampleComparison> realList = (List<RealWaterSampleComparison>) resultMap.get("list");
				realList.add(realWaterSampleComparison);
			}
		}
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(result);
	}

	@SuppressWarnings("unchecked")
	@Override
	public ResultEntity integratedInterventionData(QualityParamSelectBo qualityParamSelectBo, PageObject pageObject) {
		boolean pageFlag = pageObject != null && pageObject.getPageNumber() != null && pageObject.getPageSize() != null;
		// 验证参数
		ResultEntity resultEntity = IVerificationService.verification(qualityParamSelectBo);
		if (resultEntity.getCode() == 1) {
			return resultEntity;
		}
		Date startDate = DateUtil.dangyueDiyitianMonth(
				DateUtil.stringToDate(qualityParamSelectBo.getStartTime(), DateUtil.DATE_SMALL_YM),
				DateUtil.DATE_FULL_STR);
		Date endDate = DateUtil.getLastDayMonthM(
				DateUtil.stringToDate(qualityParamSelectBo.getEndTime(), DateUtil.DATE_SMALL_YM),
				DateUtil.DATE_FULL_STR);
		List<String> indexList = ArrayUtils.stringToList(qualityParamSelectBo.getIndexCodes());
		List<String> stationCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
		if (pageFlag) {
			Page<IntegratedIntervention> page = PageUtil.pageConvert(
					IIntegratedInterventionRepository.findByTimeBetweenAndIndexCodeInAndStationCodeInOrderByTimeDesc(
							startDate, endDate, indexList, stationCodeList, pageObject.getPageRequest()));
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setPage(page);
		}
		List<IntegratedIntervention> list = IIntegratedInterventionRepository
				.findByTimeBetweenAndIndexCodeInAndStationCodeInOrderByTimeDesc(startDate, endDate, indexList,
						stationCodeList);
		// 封装数据
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		Map<String, Object> valueMap = new HashMap<String, Object>();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		for (IntegratedIntervention integratedIntervention : list) {
			String stationCode = integratedIntervention.getStationCode();
			String artificiaSamplingTime = DateUtil.dateToString(integratedIntervention.getArtificiaSamplingTime(),
					DateUtil.DATE_SMALL_YMDHH); // 人工采样时间
			String automaticSamplingTime = DateUtil.dateToString(integratedIntervention.getAutomaticSamplingTime(),
					DateUtil.DATE_SMALL_YMDHH); // 自动取样时间
			if (!valueMap.containsKey(stationCode + artificiaSamplingTime)) {
				List<IntegratedIntervention> realList = new ArrayList<IntegratedIntervention>();
				realList.add(integratedIntervention);
				resultMap = new HashMap<String, Object>();
				resultMap.put("artificiaSamplingTime", artificiaSamplingTime);
				resultMap.put("automaticSamplingTime", automaticSamplingTime);
				resultMap.put("list", realList);
				result.add(resultMap);
				valueMap.put(stationCode + artificiaSamplingTime, resultMap);
			} else {
				resultMap = (Map<String, Object>) valueMap.get(stationCode + artificiaSamplingTime);
				List<IntegratedIntervention> realList = (List<IntegratedIntervention>) resultMap.get("list");
				realList.add(integratedIntervention);
			}
		}
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(result);
	}

	@Override
	public ResultEntity integratedInterventionChartData(QualityParamSelectBo qualityParamSelectBo) {
		// 验证参数
		ResultEntity resultEntity = IVerificationService.verification(qualityParamSelectBo);
		if (resultEntity.getCode() == 1) {
			return resultEntity;
		}
		Date startDate = DateUtil.dangyueDiyitianMonth(
				DateUtil.stringToDate(qualityParamSelectBo.getStartTime(), DateUtil.DATE_SMALL_YM),
				DateUtil.DATE_FULL_STR);
		Date endDate = DateUtil.getLastDayMonthM(
				DateUtil.stringToDate(qualityParamSelectBo.getEndTime(), DateUtil.DATE_SMALL_YM),
				DateUtil.DATE_FULL_STR);
		List<String> indexList = ArrayUtils.stringToList(qualityParamSelectBo.getIndexCodes());
		List<String> stationCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
		List<IntegratedIntervention> list = IIntegratedInterventionRepository
				.findByTimeBetweenAndIndexCodeInAndStationCodeInOrderByTimeAsc(startDate, endDate, indexList,
						stationCodeList);
		// 数据封装
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(); // 返回的结果集
		for (String string : indexList) { // 前端传入的监测项目遍历
			Map<String, Object> map = new HashMap<String, Object>();
			// X轴数据
			List<String> xAxisList = list.stream().filter(z -> string.equals(z.getIndexCode()))
					.map(z -> DateUtil.dateToString(z.getAutomaticSamplingTime(), DateUtil.DATE_SMALL_YMDHMS))
					.collect(Collectors.toList());
			// Y轴数据
			List<Double> yAxisList = list.stream().filter(z -> string.equals(z.getIndexCode()))
					.map(z -> Double.valueOf(z.getError())).collect(Collectors.toList());
			map.put("name", QualityProjectEnum.getName(string));
			map.put("xAxis", xAxisList);
			map.put("yAxis", yAxisList);
			map.put("upper", TechnicalCequirementEnum.SPAN_DRIFT_UPPER.getCode());
			map.put("lower", TechnicalCequirementEnum.SPAN_DRIFT_LOWER.getCode());
			result.add(map);
		}
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(result);
	}

	@Override
	public ResultEntity recoveryData(QualityParamSelectBo qualityParamSelectBo, PageObject pageObject) {
		boolean pageFlag = pageObject != null && pageObject.getPageNumber() != null && pageObject.getPageSize() != null;
		// 验证参数
		ResultEntity resultEntity = IVerificationService.verification(qualityParamSelectBo);
		if (resultEntity.getCode() == 1) {
			return resultEntity;
		}
		Date startDate = DateUtil.dangyueDiyitianMonth(
				DateUtil.stringToDate(qualityParamSelectBo.getStartTime(), DateUtil.DATE_SMALL_YM),
				DateUtil.DATE_FULL_STR);
		Date endDate = DateUtil.getLastDayMonthM(
				DateUtil.stringToDate(qualityParamSelectBo.getEndTime(), DateUtil.DATE_SMALL_YM),
				DateUtil.DATE_FULL_STR);
		List<String> indexList = ArrayUtils.stringToList(qualityParamSelectBo.getIndexCodes());
		List<String> stationCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
		if (pageFlag) {
			Page<Recovery> page = PageUtil.pageConvert(
					IRecoveryRepository.findByTimeBetweenAndIndexCodeInAndStationCodeInOrderByTimeDesc(startDate,
							endDate, indexList, stationCodeList, pageObject.getPageRequest()));
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setPage(page);
		}
		List<Recovery> list = IRecoveryRepository.findByTimeBetweenAndIndexCodeInAndStationCodeInOrderByTimeDesc(
				startDate, endDate, indexList, stationCodeList);
		// 封装数据
//		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
//		Map<String, Object> valueMap = new HashMap<String, Object>();
//		Map<String, Object> resultMap = new HashMap<String, Object>();
//		for (Recovery recovery : list) {
//			String stationCode = recovery.getStationCode();
//			String time = DateUtil.dateToString(recovery.getTime(), DateUtil.DATE_SMALL_STR);
//			if (!valueMap.containsKey(stationCode + time)) {
//				List<Recovery> realList = new ArrayList<Recovery>();
//				realList.add(recovery);
//				resultMap = new HashMap<String, Object>();
//				resultMap.put("time", time);
//				resultMap.put("list", realList);
//				result.add(resultMap);
//				valueMap.put(stationCode + time, resultMap);
//			} else {
//				resultMap = (Map<String, Object>) valueMap.get(stationCode + time);
//				List<Recovery> realList = (List<Recovery>) resultMap.get("list");
//				realList.add(recovery);
//			}
//		}
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
	}

	@Override
	public ResultEntity recoveryRatioData(QualityParamSelectBo qualityParamSelectBo) {
		// 验证参数
		ResultEntity resultEntity = IVerificationService.verification(qualityParamSelectBo);
		if (resultEntity.getCode() == 1) {
			return resultEntity;
		}
		Date startDate = DateUtil.dangyueDiyitianMonth(
				DateUtil.stringToDate(qualityParamSelectBo.getStartTime(), DateUtil.DATE_SMALL_YM),
				DateUtil.DATE_FULL_STR);
		Date endDate = DateUtil.getLastDayMonthM(
				DateUtil.stringToDate(qualityParamSelectBo.getEndTime(), DateUtil.DATE_SMALL_YM),
				DateUtil.DATE_FULL_STR);
		List<String> indexList = ArrayUtils.stringToList(qualityParamSelectBo.getIndexCodes());
		List<String> stationCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
		List<Recovery> list = IRecoveryRepository
				.findByTimeBetweenAndIndexCodeInAndStationCodeInOrderByRecoveryValueAsc(startDate, endDate, indexList,
						stationCodeList);
		// 封装相关数据
		long count = list.size();
		long qualifiedCount = list.stream().filter(l -> l.getQualification().equals("合格")).count(); // 合格数
		long noQualifiedCount = count - qualifiedCount; // 不合格数
		String qualifiedRate = "-";
		if (count != 0 && qualifiedCount != 0) {
			qualifiedRate = String.valueOf(ScaleUtil.sciCalFive((double) qualifiedCount * 100 / count, 1)) + "%";
		}
		int one = 0, two = 0, three = 0, four = 0, other = 0; // 四个区间的个数
		Double minValue = 0.0, maxValue = 0.0;
		List<Double> recoveryValueList = list.stream().map(l -> Double.valueOf(
				(l.getRecoveryValue() == null || "-".equals(l.getRecoveryValue())) ? "0" : l.getRecoveryValue()))
				.collect(Collectors.toList());
		// 最小值最大值
		if (recoveryValueList.size() != 0) {
			minValue = Collections.min(recoveryValueList);
			maxValue = Collections.max(recoveryValueList);
		}
		for (int i = 0; i < list.size(); i++) {
			Double recoveryValue = Double.valueOf(
					(list.get(i).getRecoveryValue() == null || "-".equals(list.get(i).getRecoveryValue())) ? "0"
							: list.get(i).getRecoveryValue());
			if (recoveryValue > 90 && recoveryValue <= 100) {
				one++;
			} else if (recoveryValue > 80 && recoveryValue <= 120) {
				two++;
			} else if (recoveryValue > 70 && recoveryValue <= 130) {
				three++;
			} else if (recoveryValue > 60 && recoveryValue <= 150) {
				four++;
			} else {
				other++;
			}
		}
		// 封装回收率各范围的总项次值
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("one", one);
		resultMap.put("two", two);
		resultMap.put("three", three);
		resultMap.put("four", four);
		resultMap.put("other", other);
		Map<String, Object> resultRatioMap = new HashMap<String, Object>();
		if (one != 0 && count != 0) {
			resultRatioMap.put("one", String.valueOf(ScaleUtil.sciCalFive((double) one * 100 / count, 1)) + "%");
		} else {
			resultRatioMap.put("one", "");
		}
		if (two != 0 && count != 0) {
			resultRatioMap.put("two", String.valueOf(ScaleUtil.sciCalFive((double) two * 100 / count, 1)) + "%");
		} else {
			resultRatioMap.put("two", "");
		}
		if (three != 0 && count != 0) {
			resultRatioMap.put("three", String.valueOf(ScaleUtil.sciCalFive((double) three * 100 / count, 1)) + "%");
		} else {
			resultRatioMap.put("three", "");
		}
		if (four != 0 && count != 0) {
			resultRatioMap.put("four", String.valueOf(ScaleUtil.sciCalFive((double) four * 100 / count, 1)) + "%");
		} else {
			resultRatioMap.put("four", "");
		}
		if (other != 0 && count != 0) {
			resultRatioMap.put("other", String.valueOf(ScaleUtil.sciCalFive((double) other * 100 / count, 1)) + "%");
		} else {
			resultRatioMap.put("other", "");
		}
		
//		resultRatioMap.put("two", String.valueOf(ScaleUtil.sciCalFive((double) two * 100 / count, 1)) + "%");
//		resultRatioMap.put("three", String.valueOf(ScaleUtil.sciCalFive((double) three * 100 / count, 1)) + "%");
//		resultRatioMap.put("four", String.valueOf(ScaleUtil.sciCalFive((double) four * 100 / count, 1)) + "%");
//		resultRatioMap.put("other", String.valueOf(ScaleUtil.sciCalFive((double) other * 100 / count, 1)) + "%");
		List<Map<String, Object>> tableList = new ArrayList<Map<String, Object>>(); // 表格结果集
		tableList.add(resultMap);
		tableList.add(resultRatioMap);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("count", count);
		result.put("qualifiedCount", qualifiedCount);
		result.put("noQualifiedCount", noQualifiedCount);
		result.put("qualifiedRate", qualifiedRate);
		result.put("minValue", minValue);
		result.put("maxValue", maxValue);
		result.put("list", tableList);
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setInfo(result);
	}

	@Override
	public ResultEntity recoveryChartData(QualityParamSelectBo qualityParamSelectBo) {
		// 验证参数
		ResultEntity resultEntity = IVerificationService.verification(qualityParamSelectBo);
		if (resultEntity.getCode() == 1) {
			return resultEntity;
		}
		Date startDate = DateUtil.dangyueDiyitianMonth(
				DateUtil.stringToDate(qualityParamSelectBo.getStartTime(), DateUtil.DATE_SMALL_YM),
				DateUtil.DATE_FULL_STR);
		Date endDate = DateUtil.getLastDayMonthM(
				DateUtil.stringToDate(qualityParamSelectBo.getEndTime(), DateUtil.DATE_SMALL_YM),
				DateUtil.DATE_FULL_STR);
		String indexCode = qualityParamSelectBo.getIndexCodes();
		List<String> indexList = ArrayUtils.stringToList(indexCode);
		List<String> stationCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
		List<Recovery> list = IRecoveryRepository.findByTimeBetweenAndIndexCodeInAndStationCodeInOrderByTimeAsc(
				startDate, endDate, indexList, stationCodeList);
		// 数据封装
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(); // 返回的结果集
		Map<String, Object> map = new HashMap<String, Object>();
		// X轴数据
		List<String> xAxisList = list.stream().filter(z -> indexCode.equals(z.getIndexCode()))
				.map(z -> DateUtil.dateToString(z.getTime(), DateUtil.DATE_SMALL_STR)).collect(Collectors.toList());
		// Y轴数据
		List<Double> yAxisList = list.stream().filter(z -> indexCode.equals(z.getIndexCode()))
				.map(z -> Double.valueOf("-".equals(z.getRecoveryValue()) ? "0" : z.getRecoveryValue())).collect(Collectors.toList());
		map.put("name", QualityProjectEnum.getName(indexCode));
		map.put("xAxis", xAxisList);
		map.put("yAxis", yAxisList);
		map.put("upper", TechnicalCequirementEnum.RECOVERY_UPPER.getCode());
		map.put("lower", TechnicalCequirementEnum.RECOVERY_LOWER.getCode());
		result.add(map);
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(result);
	}

	@Override
	public ResultEntity recoveryBarChartData(QualityParamSelectBo qualityParamSelectBo) {
		// 验证参数
		ResultEntity resultEntity = IVerificationService.verification(qualityParamSelectBo);
		if (resultEntity.getCode() == 1) {
			return resultEntity;
		}
		Date startDate = DateUtil.dangyueDiyitianMonth(
				DateUtil.stringToDate(qualityParamSelectBo.getStartTime(), DateUtil.DATE_SMALL_YM),
				DateUtil.DATE_FULL_STR);
		Date endDate = DateUtil.getLastDayMonthM(
				DateUtil.stringToDate(qualityParamSelectBo.getEndTime(), DateUtil.DATE_SMALL_YM),
				DateUtil.DATE_FULL_STR);
		String indexCode = qualityParamSelectBo.getIndexCodes();
		List<String> indexList = ArrayUtils.stringToList(indexCode);
		List<String> stationCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
		List<Recovery> list = IRecoveryRepository.findByTimeBetweenAndIndexCodeInAndStationCodeInOrderByTimeAsc(
				startDate, endDate, indexList, stationCodeList);
		// 将时间转换成字符串型
		list.stream().forEach(l -> l.setToCharTime(DateUtil.dateToString(l.getTime(), DateUtil.DATE_SMALL_YM)));
		// 获取x轴数据并过滤重复的月份日期数据
		List<String> timeList = list.stream().map(l -> l.getToCharTime()).distinct().collect(Collectors.toList());
		// 根据字符串时间分组
		Map<String, List<Recovery>> groups = list.stream().collect(Collectors.groupingBy(Recovery::getToCharTime));
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(); // 返回的结果集
		List<Long> countList = new ArrayList<Long>();
		Map<String, Object> countMap = new HashMap<String, Object>();
		List<Long> qualifiedCountList = new ArrayList<Long>();
		Map<String, Object> qualifiedCountMap = new HashMap<String, Object>();
		List<Long> noQualifiedCountList = new ArrayList<Long>();
		Map<String, Object> noQualifiedCountMap = new HashMap<String, Object>();
		List<Double> qualifiedRateList = new ArrayList<Double>();
		Map<String, Object> qualifiedRateMap = new HashMap<String, Object>();
		for (String key : groups.keySet()) {
			List<Recovery> recoveryList = groups.get(key);
			long count = recoveryList.stream().count(); // 月份内的总数
			long qualifiedCount = recoveryList.stream().filter(l -> l.getQualification().equals("合格")).count(); // 月份内的合格数
			long noQualifiedCount = count - qualifiedCount; // 月份内不合格数
			Double qualifiedRate = Double.valueOf(ScaleUtil.sciCalSix((double) qualifiedCount * 100 / count, 1));
			countList.add(count);
			countMap.put("name", "总次数");
			countMap.put("type", "bar");
			countMap.put("data", countList);
			qualifiedCountList.add(qualifiedCount);
			qualifiedCountMap.put("name", "合格次数");
			qualifiedCountMap.put("type", "bar");
			qualifiedCountMap.put("data", qualifiedCountList);
			noQualifiedCountList.add(noQualifiedCount);
			noQualifiedCountMap.put("name", "不合格次数");
			noQualifiedCountMap.put("type", "bar");
			noQualifiedCountMap.put("data", noQualifiedCountList);
			qualifiedRateList.add(qualifiedRate);
			qualifiedRateMap.put("name", "合格率");
			qualifiedRateMap.put("type", "bar");
			qualifiedRateMap.put("data", qualifiedRateList);
		}
		result.add(countMap);
		result.add(qualifiedCountMap);
		result.add(noQualifiedCountMap);
		result.add(qualifiedRateMap);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("xAxis", timeList);
		resultMap.put("series", result);
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setInfo(resultMap);
	}

	@Override
	public ResultEntity verificationOfStandardSamplesData(QualityParamSelectBo qualityParamSelectBo,
			PageObject pageObject) {
		boolean pageFlag = pageObject != null && pageObject.getPageNumber() != null && pageObject.getPageSize() != null;
		// 验证参数
		ResultEntity resultEntity = IVerificationService.verification(qualityParamSelectBo);
		if (resultEntity.getCode() == 1) {
			return resultEntity;
		}
		Date startDate = DateUtil.dangyueDiyitianMonth(
				DateUtil.stringToDate(qualityParamSelectBo.getStartTime(), DateUtil.DATE_SMALL_YM),
				DateUtil.DATE_FULL_STR);
		Date endDate = DateUtil.getLastDayMonthM(
				DateUtil.stringToDate(qualityParamSelectBo.getEndTime(), DateUtil.DATE_SMALL_YM),
				DateUtil.DATE_FULL_STR);
		List<String> indexList = ArrayUtils.stringToList(qualityParamSelectBo.getIndexCodes());
		List<String> stationCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
		if (pageFlag) {
			Page<VerificationOfStandardSample> page = PageUtil.pageConvert(IVerificationOfStandardSampleRepository
					.findByTimeBetweenAndIndexCodeInAndStationCodeInOrderByTimeDesc(startDate, endDate, indexList,
							stationCodeList, pageObject.getPageRequest()));
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setPage(page);
		}
		List<VerificationOfStandardSample> list = IVerificationOfStandardSampleRepository
				.findByTimeBetweenAndIndexCodeInAndStationCodeInOrderByTimeDesc(startDate, endDate, indexList,
						stationCodeList);
		// 封装数据
//		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
//		Map<String, Object> valueMap = new HashMap<String, Object>();
//		Map<String, Object> resultMap = new HashMap<String, Object>();
//		for (VerificationOfStandardSample verificationOfStandardSample : list) {
//			String stationCode = verificationOfStandardSample.getStationCode();
//			String time = DateUtil.dateToString(verificationOfStandardSample.getTime(), DateUtil.DATE_SMALL_STR);
//			if (!valueMap.containsKey(stationCode + time)) {
//				List<VerificationOfStandardSample> realList = new ArrayList<VerificationOfStandardSample>();
//				realList.add(verificationOfStandardSample);
//				resultMap = new HashMap<String, Object>();
//				resultMap.put("time", time);
//				resultMap.put("list", realList);
//				result.add(resultMap);
//				valueMap.put(stationCode + time, resultMap);
//			} else {
//				resultMap = (Map<String, Object>) valueMap.get(stationCode + time);
//				List<VerificationOfStandardSample> realList = (List<VerificationOfStandardSample>) resultMap
//						.get("list");
//				realList.add(verificationOfStandardSample);
//			}
//		}
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
	}
	
	@Override
	public ResultEntity verificationOfStandardSamplesRatioData(QualityParamSelectBo qualityParamSelectBo) {
		// 验证参数
		ResultEntity resultEntity = IVerificationService.verification(qualityParamSelectBo);
		if (resultEntity.getCode() == 1) {
			return resultEntity;
		}
		Date startDate = DateUtil.dangyueDiyitianMonth(
				DateUtil.stringToDate(qualityParamSelectBo.getStartTime(), DateUtil.DATE_SMALL_YM),
				DateUtil.DATE_FULL_STR);
		Date endDate = DateUtil.getLastDayMonthM(
				DateUtil.stringToDate(qualityParamSelectBo.getEndTime(), DateUtil.DATE_SMALL_YM),
				DateUtil.DATE_FULL_STR);
		List<String> indexList = ArrayUtils.stringToList(qualityParamSelectBo.getIndexCodes());
		List<String> stationCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
		List<VerificationOfStandardSample> list = IVerificationOfStandardSampleRepository
				.findByTimeBetweenAndIndexCodeInAndStationCodeInOrderByTimeAsc(startDate, endDate, indexList,
						stationCodeList);
		// 封装相关数据
		long count = list.size();
		long qualifiedCount = list.stream().filter(l -> l.getQualification().equals("合格")).count(); // 合格数
		long noQualifiedCount = count - qualifiedCount; // 不合格数
		String qualifiedRate = "-";
		if (count != 0 && qualifiedCount != 0) {
			qualifiedRate = String.valueOf(ScaleUtil.sciCalFive((double) qualifiedCount * 100 / count, 1)) + "%";
		}
		int one = 0, two = 0, three = 0, four = 0, other = 0; // 四个区间的个数
		Double minValue = 0.0, maxValue = 0.0;
		List<Double> standardValueList = list.stream().map(l -> Double.valueOf("-".equals(l.getError()) ? "0" : l.getError())).collect(Collectors.toList());
		// 最小值最大值
		if (standardValueList.size() != 0) {
			minValue = Collections.min(standardValueList);
			maxValue = Collections.max(standardValueList);
		}
		for (int i = 0; i < list.size(); i++) {
			Double error = Double.valueOf("-".equals(list.get(i).getError()) ? "0" : list.get(i).getError());
			if (error > 90 && error <= 110) {
				one++;
			} else if (error > 80 && error <= 120) {
				two++;
			} else if (error > 70 && error <= 130) {
				three++;
			} else if (error > 60 && error <= 150) {
				four++;
			} else {
				other++;
			}
		}
		// 封装标样核查各范围的总项次值
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("one", one);
		resultMap.put("two", two);
		resultMap.put("three", three);
		resultMap.put("four", four);
		resultMap.put("other", other);
		Map<String, Object> resultRatioMap = new HashMap<String, Object>();
		if (one != 0 && count != 0) {
			resultRatioMap.put("one", String.valueOf(ScaleUtil.sciCalFive((double) one * 100 / count, 1)) + "%");
		} else {
			resultRatioMap.put("one", "");
		}
		if (two != 0 && count != 0) {
			resultRatioMap.put("two", String.valueOf(ScaleUtil.sciCalFive((double) two * 100 / count, 1)) + "%");
		} else {
			resultRatioMap.put("two", "");
		}
		if (three != 0 && count != 0) {
			resultRatioMap.put("three", String.valueOf(ScaleUtil.sciCalFive((double) three * 100 / count, 1)) + "%");
		} else {
			resultRatioMap.put("three", "");
		}
		if (four != 0 && count != 0) {
			resultRatioMap.put("four", String.valueOf(ScaleUtil.sciCalFive((double) four * 100 / count, 1)) + "%");
		} else {
			resultRatioMap.put("four", "");
		}
		if (other != 0 && count != 0) {
			resultRatioMap.put("other", String.valueOf(ScaleUtil.sciCalFive((double) other * 100 / count, 1)) + "%");
		} else {
			resultRatioMap.put("other", "");
		}
		List<Map<String, Object>> tableList = new ArrayList<Map<String, Object>>(); // 表格结果集
		tableList.add(resultMap);
		tableList.add(resultRatioMap);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("count", count);
		result.put("qualifiedCount", qualifiedCount);
		result.put("noQualifiedCount", noQualifiedCount);
		result.put("qualifiedRate", qualifiedRate);
		result.put("minValue", minValue);
		result.put("maxValue", maxValue);
		result.put("list", tableList);
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setInfo(result);
	}

	@Override
	public ResultEntity verificationOfStandardSamplesChartData(QualityParamSelectBo qualityParamSelectBo) {
		// 验证参数
		ResultEntity resultEntity = IVerificationService.verification(qualityParamSelectBo);
		if (resultEntity.getCode() == 1) {
			return resultEntity;
		}
		Date startDate = DateUtil.dangyueDiyitianMonth(
				DateUtil.stringToDate(qualityParamSelectBo.getStartTime(), DateUtil.DATE_SMALL_YM),
				DateUtil.DATE_FULL_STR);
		Date endDate = DateUtil.getLastDayMonthM(
				DateUtil.stringToDate(qualityParamSelectBo.getEndTime(), DateUtil.DATE_SMALL_YM),
				DateUtil.DATE_FULL_STR);
		String indexCode = qualityParamSelectBo.getIndexCodes();
		List<String> indexList = ArrayUtils.stringToList(indexCode);
		List<String> stationCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
		List<VerificationOfStandardSample> list = IVerificationOfStandardSampleRepository
				.findByTimeBetweenAndIndexCodeInAndStationCodeInOrderByTimeAsc(startDate, endDate, indexList,
						stationCodeList);
		// 数据封装
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(); // 返回的结果集
		Map<String, Object> map = new HashMap<String, Object>();
		// X轴数据
		List<String> xAxisList = list.stream().filter(z -> indexCode.equals(z.getIndexCode()))
				.map(z -> DateUtil.dateToString(z.getTime(), DateUtil.DATE_SMALL_STR)).collect(Collectors.toList());
		// Y轴数据
		List<Double> yAxisList = list.stream().filter(z -> indexCode.equals(z.getIndexCode()))
				.map(z -> Double.valueOf("-".equals(z.getError().toString()) ? "0" : z.getError())).collect(Collectors.toList());
		map.put("name", QualityProjectEnum.getName(indexCode));
		map.put("xAxis", xAxisList);
		map.put("yAxis", yAxisList);
		map.put("upper", TechnicalCequirementEnum.SPAN_DRIFT_UPPER.getCode());
		map.put("lower", TechnicalCequirementEnum.SPAN_DRIFT_LOWER.getCode());
		result.add(map);
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(result);
	}

	@Override
	public ResultEntity verificationOfStandardSamplesBarChartData(QualityParamSelectBo qualityParamSelectBo) {
		// 验证参数
		ResultEntity resultEntity = IVerificationService.verification(qualityParamSelectBo);
		if (resultEntity.getCode() == 1) {
			return resultEntity;
		}
		Date startDate = DateUtil.dangyueDiyitianMonth(
				DateUtil.stringToDate(qualityParamSelectBo.getStartTime(), DateUtil.DATE_SMALL_YM),
				DateUtil.DATE_FULL_STR);
		Date endDate = DateUtil.getLastDayMonthM(
				DateUtil.stringToDate(qualityParamSelectBo.getEndTime(), DateUtil.DATE_SMALL_YM),
				DateUtil.DATE_FULL_STR);
		String indexCode = qualityParamSelectBo.getIndexCodes();
		List<String> indexList = ArrayUtils.stringToList(indexCode);
		List<String> stationCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
		List<VerificationOfStandardSample> list = IVerificationOfStandardSampleRepository
				.findByTimeBetweenAndIndexCodeInAndStationCodeInOrderByTimeAsc(startDate, endDate, indexList,
						stationCodeList);
		// 将时间转换成字符串型
		list.stream().forEach(l -> l.setToCharTime(DateUtil.dateToString(l.getTime(), DateUtil.DATE_SMALL_YM)));
		// 获取x轴数据并过滤重复的月份日期数据
		List<String> timeList = list.stream().map(l -> l.getToCharTime()).distinct().collect(Collectors.toList());
		// 根据字符串时间分组
		Map<String, List<VerificationOfStandardSample>> groups = list.stream()
				.collect(Collectors.groupingBy(VerificationOfStandardSample::getToCharTime));
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(); // 返回的结果集
		List<Long> countList = new ArrayList<Long>();
		Map<String, Object> countMap = new HashMap<String, Object>();
		List<Long> qualifiedCountList = new ArrayList<Long>();
		Map<String, Object> qualifiedCountMap = new HashMap<String, Object>();
		List<Long> noQualifiedCountList = new ArrayList<Long>();
		Map<String, Object> noQualifiedCountMap = new HashMap<String, Object>();
		List<Double> qualifiedRateList = new ArrayList<Double>();
		Map<String, Object> qualifiedRateMap = new HashMap<String, Object>();
		for (String key : groups.keySet()) {
			List<VerificationOfStandardSample> recoveryList = groups.get(key);
			long count = recoveryList.stream().count(); // 月份内的总数
			long qualifiedCount = recoveryList.stream().filter(l -> l.getQualification().equals("合格")).count(); // 月份内的合格数
			long noQualifiedCount = count - qualifiedCount; // 月份内不合格数
			Double qualifiedRate = Double.valueOf(ScaleUtil.sciCalFive((double) qualifiedCount * 100 / count, 1));
			countList.add(count);
			countMap.put("name", "总次数");
			countMap.put("type", "bar");
			countMap.put("data", countList);
			qualifiedCountList.add(qualifiedCount);
			qualifiedCountMap.put("name", "合格次数");
			qualifiedCountMap.put("type", "bar");
			qualifiedCountMap.put("data", qualifiedCountList);
			noQualifiedCountList.add(noQualifiedCount);
			noQualifiedCountMap.put("name", "不合格次数");
			noQualifiedCountMap.put("type", "bar");
			noQualifiedCountMap.put("data", noQualifiedCountList);
			qualifiedRateList.add(qualifiedRate);
			qualifiedRateMap.put("name", "合格率");
			qualifiedRateMap.put("type", "bar");
			qualifiedRateMap.put("data", qualifiedRateList);
		}
		result.add(countMap);
		result.add(qualifiedCountMap);
		result.add(noQualifiedCountMap);
		result.add(qualifiedRateMap);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("xAxis", timeList);
		resultMap.put("series", result);
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setInfo(resultMap);
	}

}
